package br.com.searchpeople.fragment;

import java.io.ByteArrayOutputStream;

import br.com.searchpeople.R;
import br.com.searchpeople.activity.ShowPostActivity;
import br.com.searchpeople.adapter.PostAdapter;
import br.com.searchpeople.database.SingletonPostBD;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class TabMeusPostsFragment extends Fragment {

	private ListView listView;
	private PostAdapter adapter;
	private Context ctx;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		if (container == null) {
			return null;
		}
		View view = inflater.inflate(R.layout.activity_timeline, container,
				false);

		adapter = new PostAdapter(getActivity().getApplicationContext(),
				SingletonPostBD.getInstance(
						getActivity().getApplicationContext()).getAllPosts());
		listView = (ListView) view.findViewById(R.id.listview_Timeline);
		listView.setAdapter(adapter);
		registerForContextMenu(listView);
		ctx = view.getContext();
		listView.setClickable(true);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent it = new Intent(ctx, ShowPostActivity.class);
				it.putExtra(
						"name",
						SingletonPostBD
								.getInstance(
										getActivity().getApplicationContext())
								.getPostCollection().get(arg2).getName());
				it.putExtra(
						"description",
						SingletonPostBD
								.getInstance(
										getActivity().getApplicationContext())
								.getPostCollection().get(arg2).getDescription());
				it.putExtra(
						"city",
						SingletonPostBD
								.getInstance(
										getActivity().getApplicationContext())
								.getPostCollection().get(arg2).getCity());
				it.putExtra(
						"state",
						SingletonPostBD
								.getInstance(
										getActivity().getApplicationContext())
								.getPostCollection().get(arg2).getState());
				it.putExtra(
						"country",
						SingletonPostBD
								.getInstance(
										getActivity().getApplicationContext())
								.getPostCollection().get(arg2).getCountry());
				it.putExtra(
						"phone",
						SingletonPostBD
								.getInstance(
										getActivity().getApplicationContext())
								.getPostCollection().get(arg2).getPhone());
				it.putExtra(
						"email",
						SingletonPostBD
								.getInstance(
										getActivity().getApplicationContext())
								.getPostCollection().get(arg2).getEmail());
				Bitmap b = SingletonPostBD
						.getInstance(getActivity().getApplicationContext())
						.getPostCollection().get(arg2).getImg();
				ByteArrayOutputStream bs = new ByteArrayOutputStream();
				b.compress(Bitmap.CompressFormat.PNG, 100, bs);
				it.putExtra("byteArray", bs.toByteArray());
				startActivity(it);
			}

		});
		return view;
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		adapter = new PostAdapter(getActivity().getApplicationContext(),
				SingletonPostBD.getInstance(
						getActivity().getApplicationContext())
						.getAllPosts());
		listView = (ListView) getView().findViewById(R.id.listview_Timeline);
		listView.setAdapter(adapter);
		registerForContextMenu(listView);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Selecione a ação");

		MenuInflater inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.menu_context_share, menu);

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int id = (int) info.id;
		String phone = SingletonPostBD
				.getInstance(getActivity().getApplicationContext())
				.getPostCollection().get(id).getPhone();
		String email = SingletonPostBD
				.getInstance(getActivity().getApplicationContext())
				.getPostCollection().get(id).getEmail();

		switch (item.getItemId()) {
		case R.id.action_share:
			return true;
		case R.id.action_call:
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:" + phone));
			startActivity(callIntent);
			return true;
		case R.id.action_sendsms:
			Intent smsIntent = new Intent(Intent.ACTION_VIEW);
			smsIntent.setType("vnd.android-dir/mms-sms");
			smsIntent.putExtra("address", phone);
			startActivity(smsIntent);
			return true;
		case R.id.action_sendemail:
			Uri uri = Uri.parse("mailto:" + email);
			Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
			emailIntent.putExtra(
					Intent.EXTRA_SUBJECT,
					"Possibilidade de ter encontrado "
							+ SingletonPostBD
									.getInstance(
											getActivity()
													.getApplicationContext())
									.getPostCollection().get(id).getName());
			startActivity(emailIntent);
			return true;
		default:
			return super.onContextItemSelected(item);

		}
	}

}
